package org.example;

import java.util.Scanner;

public class StudentManagement {
    //学生对象数组
    private Student[] stus;
    //实际学生个数
    private int counter;
    //键盘录入对象
    public Scanner in;

    //无参构造器：默认容量100
    public StudentManagement() {
        stus = new Student[100];
        in = new Scanner(System.in);
    }

    //有参构造器：size表示初始容量
    public StudentManagement(int size) {
        stus = new Student[size];
        in = new Scanner(System.in);
    }

    //输出 操作提示
    public void prompt() {
        System.out.println("********************");
        System.out.println("*Student Management*");
        System.out.println("********************");
        System.out.println("* 1)Add");
        System.out.println("* 2)Delete");
        System.out.println("* 3)Update");
        System.out.println("* 4)Select");
        System.out.println("* 0)Quit");
        System.out.println("********************");
        System.out.print("Please choose your operate:");
    }

    //根据用户录入的学生名称，找到学生在数组中的索引返回
    private int findStudent(String name) {
        int index = -1;
        //请补全下面核心代码
        for (int i = 0; i < this.counter; i++)
        {
            if (stus[i].getName().equals(name))
                index = i;
        }
        return index;
    }

    //下面就是增删改查 四个方法
    public void addStudent() {
        String name;
        int age;
        String gender;
        Student s;
        //判断 管理系统容量是否 已满
        if (counter >= stus.length) {
            System.out.println("Add failure,too many student!");
            //扩容  数组拷贝，System.方法   Arrays.方法
            return;
        }
        System.out.print("Please input name:");
        name = in.next();
        System.out.print("Please input age:");
        age = in.nextInt();
        System.out.print("Please input gender:");
        gender = in.next();
        s = new Student(name, age, gender);
        //关键代码
        stus[counter] = s;
        counter++;
        System.out.println("Add student success!");
    }

    public void deleteStudent() {
        if (counter <= 0)
            System.out.println("Delete failure,none student!");
        String name;
        System.out.print("Please input name:");
        name = in.next();
        int index = findStudent(name);
        if (index >= 0){
            for (int i = index; i < counter - 1; i++)
                stus[i] = stus[i + 1];
            System.out.println("delete student success");
        }
        else if (index == -1){
            System.out.println("don't find this student");
        }
    }

    public void updateStudent() {
        String name;
        System.out.print("Please input name:");
        name = in.next();
        int index = findStudent(name);
        if (index >= 0)
        {
            System.out.print("Please input new name:");
            stus[index].setName(in.next());
            System.out.print("Please input new age:");
            stus[index].setAge(in.nextInt());
            System.out.print("Please input new gender:");
            stus[index].setGender(in.next());
            System.out.println("update student success");
        }
        else
            System.out.println("don't find this student");
    }

    public void selectStudent() {
         String name;
         System.out.print("Please input name:");
         name = in.next();
         int index = findStudent(name);
         if (index == -1)
             System.out.println("none find");
         else
             System.out.println("name: " + stus[index].getName() +
                     ", age: " + stus[index].getAge() + ", gender: "
                     + stus[index].getGender());
    }
}
